package 综合应用.T3;

import cn.itcast.xml.XmlUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class T3 {

    public static void main(String[] args) throws DocumentException {

        getYMQstudent();
        System.out.println("==========================");
        getMaxCourse();
        System.out.println("==========================");
        getYmqAge();

    }
    // 5
    public static void getYmqAge() throws DocumentException {
        Map<String, Course> courseXml = getCourseXml();
        Map<String, Student> studentXml = getStudentXml();
        for (String c : courseXml.keySet()) {
            for (String s : studentXml.keySet()) {
                if (c.equals(studentXml.get(s).getCourse()) && courseXml.get(c).getName().equals("羽毛球")) {
                    if (studentXml.get(s).getAge()==18){
                        System.out.println(studentXml.get(s));
                    }
                }
            }
        }
    }

    // 4
    public static void getMaxCourse() throws DocumentException {
        Map<String, Course> courseXml = getCourseXml();
        Map<String, Student> studentXml = getStudentXml();
        Map<String,Integer>map = new HashMap<>();
        for (String c : courseXml.keySet()) {
            int count = 0;
            for (String s : studentXml.keySet()) {
                if (c.equals(studentXml.get(s).getCourse())){
                    count++;
                }
            }
            map.put(courseXml.get(c).getName(),count);
        }
        int max = 0;
        for (String s : map.keySet()) {
            max = Math.max(map.get(s),max);
        }
        for (String s : map.keySet()) {
            if (max==map.get(s)){
                System.out.println("选课人数最多的课程是："+s);
            }
        }
    }

    // 3
    public static void getYMQstudent() throws DocumentException {
        Map<String, Course> courseXml = getCourseXml();
        Map<String, Student> studentXml = getStudentXml();
        for (String c : courseXml.keySet()) {
            for (String s : studentXml.keySet()) {
                if (c.equals(studentXml.get(s).getCourse()) && courseXml.get(c).getName().equals("羽毛球")) {
                    System.out.println(s+" "+studentXml.get(s).getName()+" "+studentXml.get(s).getAge()+" "+courseXml.get(c).getName());
                }
            }
        }
    }

    // 解析 course.xml
    public static Map<String, Course> getCourseXml() throws DocumentException {
        Map<String, Course> map = new HashMap<>();
        List<Course> list = new ArrayList<>();
        SAXReader sr = new SAXReader();
        Document doc = sr.read(new File("综合应用/T3/course.xml"));
        Element rootElement = doc.getRootElement();
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            list.add(new Course(
                    element.attributeValue("id"),
                    element.elementText("name")
            ));
        }
        for (Course c : list) {
            map.put(c.getId(), c);
        }

        return map;
    }

    // 解析 student.xml
    public static Map<String, Student> getStudentXml() {
        Map<String, Student> map = new HashMap<>();
        List<Student> students = XmlUtils.readAll("综合应用/T3/student.xml", Student.class);
        for (Student s : students) {
            map.put(s.getId(), s);
        }
        return map;
    }
}